<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/5/20 0020
 * Time: 上午 11:24
 */
namespace app\index\controller;
use app\index\controller\BaseUser;
use app\model\ActivityUser;
use app\model\CompanyInvite;
use app\model\Job;
use app\model\SchoolStudent;
use app\model\SchoolStudentListen;
use app\model\UserCompanyInvite;
use app\model\UserJobSend;
use app\model\UserMessage;
use app\model\UserResume;
use app\model\UserResumeCertificate;
use app\model\UserResumeEdu;
use app\model\UserResumeExp;
use app\model\UserStudentDynamic;
use app\model\UserStudentListenComment;

class User extends BaseUser{

    /**
     * 返回简历中的头像与名字
     */
    function get_user_info(){
        $UserResume=new UserResume();
        $info=$UserResume->get_head_img_name_by_user_id($this->get_user_id());
        return $this->apiJosn($info,200);

    }
    /**
     *  返回个人简历基本资料
     */
    function get_user_resume(){

        $UserResume=new UserResume();
        $info=$UserResume->get_info($this->get_user_id());
        return $this->apiJosn($info,200);
    }

    /**
     * 保存个人简历中的基本资料
     */
    function save_user_resume(){
        $params=request()->param();
        if($params['head_img']==false){

        }

        if(if_null($params['name'])){
            return $this->apiJosn([],201,'姓名不能为空');
        }
        if(if_null($params['mobile'])){
            return $this->apiJosn([],201,'手机号码不能为空');
        }


        $params['user_id']=$this->get_user_id();
        if($params['user_id']==false){
            return $this->apiJosn([],201,'user_id不能为空');
        }
        $UserResume=new UserResume();
        $r=$UserResume->save_user_resume($params);
        if($r){
            return $this->apiJosn([],200,'操作成功');
        }else{
            return $this->apiJosn([],201,'操作失败');
        }
    }

    /**
     * 添加教育经历
     */
    function add_user_resume_edu(){
        $params=request()->param();

        $v_arrs=[
            ['key'=>'school_name','null_del'=>'学校名称不能为空'],
            ['key'=>'major','null_del'=>'专业不能为空'],
            ['key'=>'start_time','null_del'=>'开始时间不能为空'],
            ['key'=>'end_time','null_del'=>'结束不能为空'],
            ['key'=>'edu_type','null_del'=>'学历不能为空'],
        ];
        $tip_arr=arr_null_deal($params,$v_arrs);

        if($tip_arr['code']==1){
            return $this->apiJosn([],201,$tip_arr['tip']);
        }

        $params['user_id']=$this->get_user_id();

        $UserResumeEdu=new UserResumeEdu();
        $r=$UserResumeEdu->add_edu($params);
        if($r){
            return $this->apiJosn([],200,'添加成功');
        }else{
            return $this->apiJosn([],201,'添加失败');
        }

    }

    /**
     * 根据教育经历ID,返回他对应的内容
     */
    function get_user_resume_edu(){
        $params=request()->param();
        if($params['id']==false){
            $this->apiJosn([],201,'教育经历主键ID不能为空');
        }
        $UserResumeEdu=new UserResumeEdu();
        $info=$UserResumeEdu->get_edu($params['id']);
        return $this->apiJosn($info,200);
    }

    /**
     * 更新教育经历
     */
    function edit_user_resume_edu(){
        $params=request()->param();

        $v_arrs=[
            ['key'=>'school_name','null_del'=>'学校名称不能为空'],
            ['key'=>'major','null_del'=>'专业不能为空'],
            ['key'=>'start_time','null_del'=>'开始时间不能为空'],
            ['key'=>'end_time','null_del'=>'结束不能为空'],
            ['key'=>'edu_type','null_del'=>'学历不能为空'],
            ['key'=>'id','null_del'=>'id不能为空'],
        ];
        $tip_arr=arr_null_deal($params,$v_arrs);

        if($tip_arr['code']==1){
            return $this->apiJosn([],201,$tip_arr['tip']);
        }

        $params['user_id']=$this->get_user_id();

        $UserResumeEdu=new UserResumeEdu();

        $r=$UserResumeEdu->update_edu($params,$params['id']);
        if($r){
            return $this->apiJosn([],200,'更新成功');
        }else{
            return $this->apiJosn([],201,'更新失败');
        }
    }

    /**
     * 删除 简历中的教育经历
     */
    function del_user_resume_edu(){
        $params=request()->param();
        $v_arrs=[
            ['key'=>'id','null_del'=>'id不能为空'],

        ];
        $tip_arr=arr_null_deal($params,$v_arrs);

        if($tip_arr['code']==1){
            return $this->apiJosn([],201,$tip_arr['tip']);
        }
        $UserResumeEdu=new UserResumeEdu();
        $r=$UserResumeEdu->del_edu($params['id']);
        if($r){
            return $this->apiJosn([],200,'删除成功');
        }else{
            return $this->apiJosn([],201,'删除失败');
        }
    }

    /**
     * 添加简历中的工作经历
     */
    function add_user_resume_exp(){
        $params=request()->param();
        $v_arr=[
            ['key'=>'company_name','null_del'=>'公司名字不能为空'],
            ['key'=>'position','null_del'=>'担任职位不能为空'],
            ['key'=>'start_time','null_del'=>'担任职位开始时间不能为空'],
            ['key'=>'end_time','null_del'=>"担任职位结束时间不能为空"],
            ['key'=>'job_des','null_del'=>'工作描述不能为空'],

        ];
        $tip_arr=arr_null_deal($params,$v_arr);
        if($tip_arr['code']==1){
            return $this->apiJosn([],201,$tip_arr['tip']);
        }
        $UserResumeExp=new UserResumeExp();
        $params['user_id']=$this->get_user_id();
        $r=$UserResumeExp->add_user_resume_exp($params);
        if($r){
            return $this->apiJosn([],200,'操作成功');
        }else{
            return $this->apiJosn([],201,'操作失败');
        }

    }

    /**
     * 编辑简历中的工作经历
     */
    function edit_user_resume_exp(){
        $params=request()->param();
        $v_arr=[
            ['key'=>'company_name','null_del'=>'公司名字不能为空'],
            ['key'=>'position','null_del'=>'担任职位不能为空'],
            ['key'=>'start_time','null_del'=>'担任职位开始时间不能为空'],
            ['key'=>'end_time','null_del'=>"担任职位结束时间不能为空"],
            ['key'=>'job_des','null_del'=>'工作描述不能为空'],
            ['key'=>'id','null_del'=>'id不能为空'],

        ];
        $tip_arr=arr_null_deal($params,$v_arr);
        if($tip_arr['code']==1){
            return $this->apiJosn([],201,$tip_arr['tip']);
        }
        $UserResumeExp=new UserResumeExp();

        $r=$UserResumeExp->edit_user_resume_exp($params,$params['id']);
        if($r){
            return $this->apiJosn([],200,'操作成功');
        }else{
            return $this->apiJosn([],201,'操作失败');
        }
    }

    /**
     * 根据工作经历ID，返回对应的经历详情
     */
    function get_user_resume_exp(){
        $params=request()->param();
        if($params['id']==false){
            return $this->apiJosn([],201,'工作经历ID的值不能为空');
        }
        $UserResumeExp=new UserResumeExp();
        $info=$UserResumeExp->get_resume_exp_by_id($params['id']);
        return $this->apiJosn($info);
    }


    /**
     * 删除简历中的工作经历
     */
    function del_user_resume_exp(){
        $params=request()->param();
        $v_arr=[

            ['key'=>'id','null_del'=>'id不能为空'],

        ];
        $tip_arr=arr_null_deal($params,$v_arr);
        if($tip_arr['code']==1){
            return $this->apiJosn([],201,$tip_arr['tip']);
        }
        $UserResumeExp=new UserResumeExp();
        $r=$UserResumeExp->del_user_resume_exp($params['id']);
        if($r){
            return $this->apiJosn([],200,'操作成功');
        }else{
            return $this->apiJosn([],201,'操作失败');
        }
    }



    function add_user_resume_cert(){
        $params=request()->param();
        $v_arr=[

            ['key'=>'name','null_del'=>'技能/证书不能为空'],
            ['key'=>'des','null_del'=>'技能/证书内容不能为空'],

        ];
        $tip_arr=arr_null_deal($params,$v_arr);
        if($tip_arr['code']==1){
            return $this->apiJosn([],201,$tip_arr['tip']);
        }

        $params['user_id']=$this->get_user_id();
        $UserResumeCertificate=new UserResumeCertificate();
        $r=$UserResumeCertificate->add_user_resume_cer($params);
        if($r){
            return $this->apiJosn([],200,'操作成功');
        }else{
            return $this->apiJosn([],201,'操作失败');
        }
    }

    function edit_user_resume_cert(){
        $params=request()->param();
        $v_arr=[

            ['key'=>'name','null_del'=>'技能/证书不能为空'],
            ['key'=>'des','null_del'=>'技能/证书内容不能为空'],
            ['key'=>'id','null_del'=>'id不能为空'],

        ];
        $tip_arr=arr_null_deal($params,$v_arr);
        if($tip_arr['code']==1){
            return $this->apiJosn([],201,$tip_arr['tip']);
        }
        $UserResumeCertificate=new UserResumeCertificate();
        $r=$UserResumeCertificate->edit_user_resume_cer($params,$params['id']);
        if($r){
            return $this->apiJosn([],200,'操作成功');
        }else{
            return $this->apiJosn([],201,'操作失败');
        }

    }

    /**
     * 根据技能ID,返回他对应的技能详情
     */
    function get_user_resume_cert(){
        $params=request()->param();
        if($params['id']==false){
            return $this->apiJosn([],201,'技能对应主键ID不能为空');
        }
        $UserResumeCertificate=new UserResumeCertificate();
        $info=$UserResumeCertificate->get_user_resume_cert_by_id($params['id']);
        $this->apiJosn($info);
    }

    function del_user_resume_cert(){
        $params=request()->param();
        $v_arr=[


            ['key'=>'id','null_del'=>'id不能为空'],

        ];
        $tip_arr=arr_null_deal($params,$v_arr);
        if($tip_arr['code']==1){
            return $this->apiJosn([],201,$tip_arr['tip']);
        }

        $UserResumeCertificateModel=new UserResumeCertificate();
        $r=$UserResumeCertificateModel->del_user_resume_cer($params['id']);
        if($r){
            return $this->apiJosn([],200);
        }else{
            return $this->apiJosn([],201,'操作失败');
        }
    }

    /**
     * 保存简历中的评论
     */
    function save_user_resume_evaluate(){
        $params=request()->param();
        $v_arr=[

            ['key'=>'evaluate','null_del'=>'评论不能为空'],


        ];
        $tip_arr=arr_null_deal($params,$v_arr);
        if($tip_arr['code']==1){
            return $this->apiJosn([],201,$tip_arr['tip']);
        }

        $UserResume=new UserResume();
        $params['user_id']=$this->get_user_id();
        $r=$UserResume->save_evaluate($params);
        if($r){
            return $this->apiJosn([],200);
        }else{
            return $this->apiJosn([],201,'操作失败');
        }
    }

    /**
     * 保存简历中的生活照图片列表
     */
    function save_user_resuem_pic(){
        $params=request()->param();
        $v_arr=[

            ['key'=>'pic','null_del'=>'图片不为空'],


        ];
        $tip_arr=arr_null_deal($params,$v_arr);
        if($tip_arr['code']==1){
            return $this->apiJosn([],201,$tip_arr['tip']);
        }

        $UserResume=new UserResume();
        $params['user_id']=$this->get_user_id();
        $r=$UserResume->save_pic($params);
        if($r){
            return $this->apiJosn([],200);
        }else{
            return $this->apiJosn([],201,'保存失败');
        }



    }

    /**
     * 返回全资料简历预览
     */
    function get_preview_user_resume(){
        $params=request()->param();



        $UserResume=new UserResume();
        $UserResumeEdu=new UserResumeEdu();
        $UserResumeExp=new UserResumeExp();
        $UserResumeCertificate=new UserResumeCertificate();
        $info['base']=$UserResume->get_info($this->get_user_id());
        $info['edu']=$UserResumeEdu->get_arrs_by_user_id($this->get_user_id());
        $info['exp']=$UserResumeExp->get_arrs_by_user_id($this->get_user_id());
        $info['cer']=$UserResumeCertificate->get_arrs_by_user_id($this->get_user_id());
        return $this->apiJosn($info,200);
    }


    /**
     * 投递简历
     */
    function deliver_job(){
        $params=request()->param();
        $v_arr=[
            ['key'=>'id','null_del'=>"工作信息id不能为空"],
        ];
        $t_arr=arr_null_deal($params,$v_arr);
        if($t_arr['code']==1){
            return $this->apiJosn([],201,$t_arr['tip']);
        }

        $Job=new Job();
        $j_exit=$Job->if_exit_by_id($params['id']);
        if(!$j_exit){
            return $this->apiJosn([],201,'工作已经删除');
        }

        $UserJobSend=new UserJobSend();
        $r=$UserJobSend->if_deliver($this->get_user_id(),$params['id']);
        if($r==true){
            return $this->apiJosn([],201,'已经投递过');
        }

        $UserResume=new UserResume();
        $user_resume_id=$UserResume->get_id_by_user_id($this->get_user_id());
        if($user_resume_id==false){
            return $this->apiJosn([],201,'没有简历,请设置');
        }

        $r=$UserJobSend->add_deliver($this->get_user_id(),$params['id'],$user_resume_id);
        if($r){
            return $this->apiJosn([],200,'投递成功');
        }else{
            return $this->apiJosn([],201,'投递失败');
        }

    }

    /**
     * 保存求职意向
     */
    function save_exp(){

        $params=request()->param();
        $v_arr=[
            ['key'=>'exp_job','null_del'=>"期望职位不能为空"],
            ['key'=>'exp_area_id','null_del'=>'求职区域不能为空'],
            ['key'=>'exp_job_in_time','null_del'=>'到职时间不能为空'],
            ['key'=>'exp_work_type','null_del'=>'期望工作性质不能为空'],
            ['key'=>'exp_salary_type','null_del'=>'期望薪资不能为空'],
        ];
        $t_arr=arr_null_deal($params,$v_arr);
        if($t_arr['code']==1){
            return $this->apiJosn([],201,$t_arr['tip']);
        }
        unset($params['province_name']);
        unset($params['city_name']);
        unset($params['exp_work_type_name']);
        unset($params['exp_salary_type_name']);
        $User=new \app\model\User();
        $r=$User->save_exp($this->get_user_id(),$params);
        if($r){
            return $this->apiJosn([],200);
        }else{
            return $this->apiJosn([],201,'操作失败');
        }
    }

    /**
     * 返回用户的求职意向
     */
    function get_user_exp(){
        $params=request()->param();
        $User=new \app\model\User();
        $info=$User->get_exp($this->get_user_id());
        return $this->apiJosn($info);
    }

    /**
     * 取得企业邀请
     */
    function get_company_invite(){
        $params=request()->param();
        if(if_null($params['status'])){
            return $this->apiJosn([],201,'status不能为空');
        }
        $CompanyInvite=new CompanyInvite();
        $arrs=$CompanyInvite->get_company_invite_by_user_id($this->get_user_id(),$params['status']);
        return $this->apiJosn($arrs,200);
    }

    /**
     * 接受公司面试邀请
     */
    function company_invite_accept(){
        $params=request()->param();
        if(if_null($params['id'])){
            return $this->apiJosn([],201,'面试邀请ID不能为空');
        }
        $CompanyInvite=new CompanyInvite();
        $r=$CompanyInvite->change_to_accept_status($params['id']);
        if($r){
            return $this->apiJosn([],200);
        }else{
            return $this->apiJosn([],201,'操作失败');
        }

    }

    /**
     * 拒绝公司面试邀请
     */
    function company_inivite_reject(){
        $params=request()->param();
        if(if_null($params['id'])){
            return $this->apiJosn([],201,'面试邀请ID不能为空');
        }
        $CompanyInvite=new CompanyInvite();
        $r=$CompanyInvite->change_to_reject_status($params['id']);
        if($r){
            return $this->apiJosn([],200);
        }else{
            return $this->apiJosn([],201,'操作失败');
        }
    }

    /**
     * 对指定的用户发送聊天信息
     */
    function add_user_message(){
        $params=request()->param();
        if($params['user_id2']==false){
            return $this->apiJosn([],201,"接受信息的用户ID不能为空");
        }
        if(if_null($params['content'])){
            return $this->apiJosn([],201,'内容不能为空');
        }
        $UserMessage=new UserMessage();
        $r=$UserMessage->add_message($this->get_user_id(),$params['user_id2'],$params['content']);
        if($r){
            return $this->apiJosn([],200);
        }else{
            return $this->apiJosn([],201,'操作失败');
        }
    }

    /**
     * 取得用户跟另一个用户的聊天对话记录
     */
    function get_user_message(){
        $params=request()->param();
        if($params['user_id2']==false){
            return $this->apiJosn([],201,"接受聊天用户ID不能为空");
        }
        $UserMessage=new UserMessage();
        $list=$UserMessage->get_all_message_by_user_id_user_id2($this->get_user_id(),$params['user_id2']);
        foreach($list as $k=>$v){
            if($v['user_id']=$this->get_user_id()){
                $list[$k]['is_myself']=1;
                $list[$k]['user']=$v['user_id_arr'];


            }else{
                $list[$k]['is_myself']=0;
                $list[$k]['user']=$v["user_id_arr2"];
            }
            unset($list[$k]['user_id_arr']);
            unset($list[$k]['user_id2_arr']);

        }
        return $this->apiJosn($list,200);
    }

    /**
     * 变成能招聘的企业用户
     */
    function be_company_user(){
        $UserModel=new \app\model\User();
        $r=$UserModel->user_type_to_company($this->get_user_id());
        if($r){
            return $this->apiJosn([],200,'成为能招聘的企业用户');
        }else{
            return $this->apiJosn([],201,'操作失败');
        }
    }

    /**
     * 变成投简历的普通用户
     */
    function be_user(){
        $UserModel=new \app\model\User();
        $r=$UserModel->user_type_to_user($this->get_user_id());
        if($r){
            return $this->apiJosn([],200,'成为能简历的普通用户');
        }else{
            return $this->apiJosn([],201,'操作失败');
        }
    }

    /**
     * 返回用户的类型
     */
    function get_user_type(){
        $User=new \app\model\User();
        $user_type=$User->get_user_type_by_id($this->get_user_id());
        $info['user_type']=$user_type;
        $info['user_type_name']=config('web.user_type')[$user_type];
        return $this->apiJosn($info);
    }

    /**
     * 添加一条用户参加指定招聘会活动记录
     */
    function add_join_activity(){
        $params=request()->param();
        if($params['activity_id']==false){
            return $this->apiJosn([],201,'activity_id不能为空');
        }
        $ActivityUser=new ActivityUser();
        $if_join=$ActivityUser->if_join_activity($params['activity_id'],$this->get_user_id());
        if($if_join==true){
            return $this->apiJosn([],201,'不能重复提交参加记录');
        }
        $r=$ActivityUser->add_user_join_activity_log($params['activity_id'],$this->get_user_id());
        if($r){
            return $this->apiJosn([],200);
        }else{
            return $this->apiJosn([],201,'操作失败');
        }
    }


    /**
     * 返回已经对公司投递过简历记录
     */
    function get_has_deliver_log(){
        $params=request()->param();
        if($params['page']==false){
            $params['page']=1;
        }
        $UserJobSendModel=new UserJobSend();
        $list=$UserJobSendModel->get_has_deliver_job_list($params['page'],$this->get_user_id());
        return $this->apiJosn($list);
    }

    /**
     * 返回用户对用户的最近消息聊天列表
     */
    function get_recent_message_user(){
        $UserMessage=new UserMessage();
        $arrs=$UserMessage->get_recent_user($this->get_user_id());
        return $this->apiJosn($arrs);
    }

    /**
     * 返回被公司邀请的记录
     */
    function get_company_invite_log(){
        $UserCompanyInvite=new UserCompanyInvite();
        $list=$UserCompanyInvite->get_has_get_invite_log($this->get_user_id());
        return $this->apiJosn($list);
    }


    /**
     * 保存学生卡信息
     */
    function save_school_student(){
        $params=request()->param();

        $v_arrs=[
            ['key'=>'stu_name','null_del'=>'学生姓名不能为空'],
            ['key'=>'edu_type','null_del'=>'学历不能为空'],
            ['key'=>'school_id','null_del'=>'学校不能为空'],
            ['key'=>'department_name','null_del'=>'院系不能为空'],
            ['key'=>'major','null_del'=>'专业不能为空'],
            ['key'=>'enter_school_time','入学时间不能为空']
        ];
        $tip_arr=arr_null_deal($params,$v_arrs);
        if($tip_arr['code']==1){
            return $this->apiJosn([],201,$tip_arr['tip']);
        }
        $SchoolStudent=new SchoolStudent();
        $params['user_id']=$this->get_user_id();
        $r=$SchoolStudent->save_student($params);
        if($r){
            return $this->apiJosn([],200);
        }else{
            return $this->apiJosn([],201,'操作失败');
        }

    }

    /**
     * 返回学生信息卡信息
     */
    function get_student_info(){
        $SchoolStudent=new SchoolStudent();
        $info=$SchoolStudent->get_student($this->get_user_id());
        return $this->apiJosn($info);
    }


    /**
     * 发送打听信息
     */
    function add_school_student_listen(){

        $params=request()->param();


        $v_arrs=[
            ['key'=>'content','null_del'=>'内容不能为空'],
            ['key'=>'school_id','null_del'=>'学校ID不能为空']

        ];
        $tip_arr=arr_null_deal($params,$v_arrs);
        if($tip_arr['code']==1){
            return $this->apiJosn([],201,$tip_arr['tip']);
        }
        /*
        if(if_null($params['pid'])){
            return $this->apiJosn([],201,'pid不能为空');
        }
        */

        $SchoolStudentListen=new SchoolStudentListen();

        $params['user_id']=$this->get_user_id();

        $r=$SchoolStudentListen->add_data($params);
        if($r){
            return $this->apiJosn([],200);
        }else{
            return $this->apiJosn([],201,'保存失败');
        }
    }

    /**
     * 对打听信息进行评论
     */
    function add_school_student_listen_comment(){
        $params=request()->param();
        if($params['school_student_listen_id']==false){
            return $this->apiJosn([],201,'打听信息的ID不能为空');
        }
        if($params['comment']==false){
            return $this->apiJosn([],201,'评论内容不能为空');
        }

        $UserStudentListenComment=new UserStudentListenComment();
        $params['user_id']=$this->get_user_id();
        $r=$UserStudentListenComment->add_comment($params);

        if($r){
            return $this->apiJosn([],200);
        }else{
            return $this->apiJosn([],201,'添加失败');
        }

    }

    function get_school_studen_listen_comment_list(){

        $params=request()->param();
        if($params['school_student_listen_id']==false){
            return $this->apiJosn([],201,'打听信息的ID不能为空');
        }
        $UserStudentListenComment=new UserStudentListenComment();
        $list=$UserStudentListenComment->get_comment_by_pid($params['school_student_listen_id']);
        return $this->apiJosn($list);

    }

    /**
     * 发表动态
     */
    function add_dynamic(){
        $params=request()->param();
        if($params['school_id']==false){
            return $this->apiJosn([],201,'学校ID不能为空');
        }
        if($params['content']==false){
            return $this->apiJosn([],201,'内容不能为空');
        }
        $UserStudentDynamic=new UserStudentDynamic();
        $params['user_id']=$this->get_user_id();

        $r=$UserStudentDynamic->add_dynamic($params);
        if($r){
            return $this->apiJosn([],200);

        }else{
            return $this->apiJosn([],201,'操作失败');
        }

    }

    function get_dynamic_page_list(){
        $params=request()->param();
        if($params['school_id']==false){
            return $this->apiJosn([],201,'学校ID不能为空');
        }

        if($params['cur_page']==false){
            $params['cur_page']=1;
        }

        $UserStudentDynamic=new UserStudentDynamic();
        $list=$UserStudentDynamic->get_page_list($params['school_id'],$params['cur_page']);

    }




}
